/*
 * Copyright (C) 2004-2006  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"
#include "FdoWcsXmlGlobals.h"
#include <WCS/Override/FdoWcsOvCoverageDefinition.h>

FdoWcsOvCoverageDefinition::FdoWcsOvCoverageDefinition(void) :
m_pXmlContentHandler(NULL)
{
}

FdoWcsOvCoverageDefinition::~FdoWcsOvCoverageDefinition(void)
{
    FDO_SAFE_RELEASE(m_pXmlContentHandler);
}

FdoWcsOvCoverageDefinition* FdoWcsOvCoverageDefinition::Create()
{
    return new FdoWcsOvCoverageDefinition;
}

FdoString* FdoWcsOvCoverageDefinition::GetIdentifier(void) const
{
    return m_identifier;
}

void FdoWcsOvCoverageDefinition::SetIdentifier(FdoString* identifier)
{
    m_identifier = identifier;
}

FdoString* FdoWcsOvCoverageDefinition::GetImageFormat(void) const
{
    return m_format;
}

void FdoWcsOvCoverageDefinition::SetImageFormat(FdoString* format)
{
    m_format = format;
}

FdoWcsOvBoundingBoxDefinition* FdoWcsOvCoverageDefinition::GetBoundingBox(void) const
{
    return FDO_SAFE_ADDREF(m_bBox.p);
}

void FdoWcsOvCoverageDefinition::SetBoundingBox(FdoWcsOvBoundingBoxDefinition* bBox)
{
    m_bBox = FDO_SAFE_ADDREF(bBox);
    if (m_bBox) {
        m_bBox->SetParent(this);
    }
}

void FdoWcsOvCoverageDefinition::InitFromXml(FdoXmlSaxContext* pContext, FdoXmlAttributeCollection* attrs)
{
    return BaseType::InitFromXml(pContext, attrs);
}

FdoXmlSaxHandler* FdoWcsOvCoverageDefinition::XmlStartElement(
    FdoXmlSaxContext* context, 
    FdoString* uri, 
    FdoString* name, 
    FdoString* qname, 
    FdoXmlAttributeCollection* atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try 
    {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL) {
            if (FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsBoundingBox) == 0) {
                FdoWcsOvBoundingBoxDefinitionP pNewObj = FdoWcsOvBoundingBoxDefinition::Create();
                pNewObj->InitFromXml(context, atts);
                SetBoundingBox(pNewObj);
                pRet = pNewObj;
            }
            else if (FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsIdentifier) == 0 ||
                    FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsImageFormat) == 0) 
            {
                m_pXmlContentHandler = FdoXmlCharDataHandler::Create();
                pRet = m_pXmlContentHandler;
            }
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return pRet;
}

FdoBoolean FdoWcsOvCoverageDefinition::XmlEndElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname)
{
    try {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        BaseType::XmlEndElement(context, uri, name, qname);

        if (NULL != m_pXmlContentHandler)
        {
            if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsIdentifier) == 0) {
                SetIdentifier(m_pXmlContentHandler->GetString());
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsImageFormat) == 0) {
                SetImageFormat(m_pXmlContentHandler->GetString());
            }

            FDO_SAFE_RELEASE(m_pXmlContentHandler);
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return false;
}

void FdoWcsOvCoverageDefinition::_writeXml( FdoXmlWriter* xmlWriter, const FdoXmlFlags* flags )
{
    VALIDATE_ARGUMENT(xmlWriter);
    VALIDATE_ARGUMENT(flags);

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsCoverageDefinition);
    BaseType::_writeXml(xmlWriter, flags);

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsIdentifier);
    xmlWriter->WriteCharacters(GetIdentifier());
    xmlWriter->WriteEndElement();

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsImageFormat);
    xmlWriter->WriteCharacters(GetImageFormat());
    xmlWriter->WriteEndElement();

    FdoWcsOvBoundingBoxDefinitionP bBox = GetBoundingBox();
    if (bBox) {
        bBox->_writeXml(xmlWriter, flags);
    }

    xmlWriter->WriteEndElement();
}

void FdoWcsOvCoverageDefinition::Dispose(void)
{
    delete this;
}

FdoStringP FdoWcsOvCoverageDefinition::GetQualifiedName()
{
    FdoStringP ret = this->GetName ();
    FdoPtr<FdoPhysicalElementMapping> parent = this->GetParent ();
    if (parent != NULL)
    {
        FdoStringP parentQName = parent->GetQualifiedName ();
        if (parentQName.GetLength () > 0)
        {
            ret = parentQName + L"." + ret;
        }
    }

    return ret;
}